package mblog.staticfile;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import mblog.modules.blog.data.PostVO;
import mblog.modules.blog.service.PostService;
import mblog.web.controller.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.*;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("staticfile")
public class MainController extends BaseController{

    @Resource
    Configuration cfg;
    
	@Autowired
	private PostService postService;

    @GetMapping
    public String main(Model model){
        String w="Welcome FreeMarker!";
        Map root = new HashMap();
        root.put("error",w);
        freeMarkerContent(root,"card/channel/view.ftl","/static/view.html");
        System.out.println("静态化完成");
        return "";
    }
    
    @GetMapping
    @RequestMapping("/right")
    @ResponseBody
    public String genRight(){
    	String w="Welcome FreeMarker!";
        Map root = new HashMap();
        root.put("error",w);
        freeMarkerContent(root,"default/inc/rightarticle.ftl","/templates/default/inc/rightarticle.html");
        System.out.println("right 静态化完成");
    	return "右栏静态化成功";
    }
    
    @GetMapping
    @RequestMapping("/channel")
    @ResponseBody
    public String genChannel(){
    	String w="Welcome FreeMarker!";
        Map root = new HashMap();
        root.put("error",w);
        freeMarkerContent(root,"default/inc/headerchannel.ftl","/templates/default/inc/headerchannel.html");
        System.out.println("right 静态化完成");
    	return "栏目静态化成功";
    }
    
    @GetMapping
    @RequestMapping("/content")
    @ResponseBody
    public String genContent(){
    	Pageable pageable = new PageRequest(0, 300);
		Page<PostVO> page = postService.paging4Admin(pageable, 0, null, 0);
		Map<String,PostVO> root = new HashMap<String,PostVO>();
		int i =0 ;
		for(PostVO post:page.getContent()){
			i++;
			post=postService.get(post.getId());
			root.put("view",post);
			freeMarkerContent2(root,"F://content//view"+i+".html");
		}
    	return "内容静态化成功";
    }
    
    public void freeMarkerContent2(Map<String,PostVO> root,String htmlName){
        try {
            Template temp = cfg.getTemplate("card/channel/view.ftl");
            //以classpath下面的static目录作为静态页面的存储目录，同时命名生成的静态html文件名称
            String path=htmlName;
            System.out.println("静态化路径："+path);
            Writer file = new FileWriter(new File(path));
            temp.process(root, file);
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void freeMarkerContent(Map<String,Object> root,String template,String htmlName){
        try {
            Template temp = cfg.getTemplate(template);
            //以classpath下面的static目录作为静态页面的存储目录，同时命名生成的静态html文件名称
            String path=this.getClass().getResource("/").toURI().getPath()+htmlName;
            System.out.println("静态化路径："+path);
            Writer file = new FileWriter(new File(path.substring(path.indexOf("/"))));
            temp.process(root, file);
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
